package cn.net.wangyixinwen.mvvm.activity;

import static android.content.Intent.getIntent;

import android.view.LayoutInflater;

import androidx.lifecycle.Observer;

import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import cn.net.wangyixinwen.bean.NewsBean;
import cn.net.wangyixinwen.databinding.ActivityNewsDetailsBinding;
import cn.net.wangyixinwen.mvvm.vm.NewsDetailsVM;
import cn.net.wangyixinwen.util.ZujiData;

public class NewsDetailsActivity extends BaseActivity<NewsDetailsVM> {

    private ActivityNewsDetailsBinding viewBinding;

    @Override
    protected NewsDetailsVM viewModel() {
        return new NewsDetailsVM();
    }

    @Override
    protected void initView() {
        vm.newsBean = (NewsBean) getIntent().getSerializableExtra("data");
        viewBinding = ActivityNewsDetailsBinding.inflate(LayoutInflater.from(this));
        setContentView(viewBinding.getRoot());
        viewBinding.titleBar.setTitle("新闻详情");
        viewBinding.titleBar.getLeftView().setOnClickListener(v -> finish());
        initObserver();
        vm.getNewsDetails();
    }

    private void initObserver() {
        vm.event.observe(this, new Observer<NewsBean>() {
            @Override
            public void onChanged(NewsBean newsBean) {
                ZujiData.list.add(0, vm.newsBean);
                Glide.with(NewsDetailsActivity.this)
                        .load(newsBean.getImgsrc().replace(",", ":"))
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                        .into(viewBinding.ivImage);


                viewBinding.tvTitle.setText(newsBean.getTitle());
                viewBinding.tvTime.setText(newsBean.getPtime() + "\n" + "点赞:" + newsBean.getVotecount() + "  " + "回复:" + newsBean.getReplyCount() + "  ");
                viewBinding.tvType.setText(newsBean.getLtitle());
                viewBinding.tvcontent.setText(newsBean.getDigest() + "\n来源：" + newsBean.getSource());

                viewBinding.tvCommit.setOnClickListener(view -> {
                    ToastUtils.showShort("评论成功");
                });
                viewBinding.tvDianzan.setOnClickListener(view -> {
                    ToastUtils.showShort("点赞成功");
                });

                viewBinding.tvZhuanfa.setOnClickListener(view -> {
                    ToastUtils.showShort("转发成功");
                });
            }
        });

    }


}
